import {
	code2session
} from "@/common/api.js"
import {
	code2session_url
} from "@/common/backend_url.js"
export const login = async () => {
	uni.showLoading({
		mask: true,
		title: "数据初始化..."
	})
	const {
		provider
	} = await uni.getProvider({
		service: 'oauth'
	})
	//得到所属平台
	const platform = provider[0];
	//使用平台获取到相对应平台的code
	const {
		code
	} = await uni.login({
		provider: platform
	})
	//如果正确得到code,则执行交换token操作
	if (code) {
		const data = {
			provider: platform,
			code: code
		}
		const response = await uni.request({
			header: {
				"content-type": "application/json",
			},
			url: code2session_url,
			method: "POST",
			data: data,
		})
		const res = response.data
		if (res.code == 0) {
			//将token存入localstorage
			uni.setStorage({
				key: "auth",
				data: res.data
			})
		}
	}
	uni.hideLoading()
}